import React, { useState, useEffect } from 'react';
import { connect } from 'dva';
import EditTemplate from '@/components/EditTemplate';
import { timeFormat } from '@/utils/common';
import moment from 'moment';
import CapitalSource from '@/pages/components/capitalSource';
const BuildEdit = ({ location, dispatch, update, uuid }: any) => {
  const [detail, setDetail] = useState(null);
  const { id, readOnly } = location.query;

  const saveUrl = id ? '/api/topic/v1/update' : '/api/topic/v1/create';
  useEffect(() => {
    if (id) {
      if (update === false) return;
      dispatch({
        type: '$request/post',
        payload: {
          url: '/api/topic/v1/detail',
          data: {
            uuid: uuid || id,
            page: 0,
            size: 10,
          },
        },
      }).then(res => {
        console.log('储备项目申报详情：', res);
        setDetail({
          ...res.project,
          ...res,
          date: readOnly
            ? timeFormat(res.date, 'YYYY-MM-DD')
            : moment(res.date),
          createTime: timeFormat(res.project.createTime, 'YYYY-MM-DD'),
          projectUuid: res.project.uuid,
        });
      });
    }
  }, []);
  const projectChange = (uuid, data) => {
    if (update === false) return;
    dispatch({
      type: '$request/post',
      payload: {
        url: '/api/topic/v1/project/query',
        data: { uuid },
      },
    }).then(list => {
      const res = list.find(item => item.uuid == uuid);
      res.createTime = timeFormat(new Date(), 'YYYY-MM-DD');
      setDetail(res);
    });
  };
  const filters = [
    {
      title: '基础信息',
      list: [
        {
          key: id ? 'name' : 'projectUuid',
          type: id ? 'text' : 'select',
          label: '项目名称',
          asyncopt: {
            url: '/api/topic/v1/project/query',
          },
          prop: {
            onChange: (uuid, data) => {
              console.log('uuid', uuid);
              projectChange(uuid, data);
            },
          },
          rules: [{ required: true }],
          span: 12,
        },
        {
          key: 'no',
          type: 'text',
          label: '项目ID',
          placeholder: '请输入姓名',
          rules: [{ required: true }],
          span: 12,
        },
        {
          key: ['ownerOrgUnit', 'name'],
          type: 'text',
          label: '业主单位',
          span: 12,
        },
        {
          key: 'createTime',
          type: 'text',
          label: '登记日期',
          span: 12,
        },
        {
          key: 'date',
          type: 'datepicker',
          label: '立项日期',
          rules: [{ required: true }],
          span: 12,
        },
        {
          key: 'code',
          type: 'input',
          label: '项目代码',
          rules: [{ required: true }],
          span: 12,
        },
        {
          key: 'number',
          type: 'input',
          label: '审批文号',
          rules: [{ required: true }],
          span: 12,
        },
        {
          key: 'investment',
          type: 'input',
          label: '审批投资额',
          prop: {
            addonAfter: '万元',
          },
          rules: [
            {
              required: true,
              pattern: new RegExp(/^-?\d+/, 'g'),
              message: '只能输入数字',
            },
          ],
          span: 24,
        },
        {
          key: 'org',
          type: 'input',
          label: '审批单位',
          rules: [{ required: true }],
          span: 24,
        },
        {
          key: 'fileList',
          type: 'upload',
          label: '立项电子材料',
          rules: [{ required: true }],
          span: 24,
        },
      ],
    },
  ];
  const preproccess = data => {
    const fileUuidList = data.fileList
      ? data.fileList.map(item => item.uuid)
      : [];
    return {
      fileUuidList,
      ...data,
    };
  };
  return (
    <div>
      <EditTemplate
        filters={filters}
        initialValues={detail}
        updateUrl={saveUrl}
        preproccess={preproccess}
        readOnly={readOnly}
      />
    </div>
  );
};

export default connect((state: any) => state.auth)(BuildEdit);
